<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>魏远商城</title>
		<meta name="viewport" content="initial-scale=1, maximum-scale=1">
		<link rel="shortcut icon" href="/favicon.ico">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/app.css" />
		<link rel="stylesheet" href="css/sm.css">
		<link rel="stylesheet" href="css/sm-extend.css">
		<link rel="stylesheet" href="css/ionicons.css">
		<link rel="stylesheet" href="css/my-address.css">
		<link rel="stylesheet" href="css/style.css" />
		<link rel="stylesheet" href="css/my-order.css" />
	</head>
	<style>
		#1 {
			color: #fff;
			background-color: #e9af29;
			border: none;
		}
		.mui-popup-in{
			border: 1px solid #777;
		}
	</style>

	<body>
		<div class="page-group">
			<div class="page page-current">
				<!--标题栏-->
				<header class="bar bar-nav " style="width: 100%;">
					<span class="order-back mui-action-back"><img src="img/back-le.png" height="50%"/></span>
					<h1 class="title">我的收货地址</h1>
				</header>
				<!-- 工具栏 -->
				<a class="bar bar-tab add-addr external">
					添加收货地址
				</a>
				<!--这里是页面内容区域-->
				<div class="content">
					<!--<div class="card">
						<div class="card-content">
							<div class="card-content-inner">
								<p class="user-info"><span class="iconfont icon-iconfontlianxiren01"></span><span>陈锦川</span><span class="iconfont icon-shouji2"></span><span>15602268070</span></p>
								<p class="user-addr">
									<span>广东省</span><span>广州市</span><span>天河区</span><span>银汇大厦26楼蜘点集团技术部</span>
								</p>
							</div>
						</div>
						<div class="card-footer">
							<label class="pull-left active">默认地址</label>
							<label class="pull-right">
								<a class="external" href="creat-address.html"><i class="iconfont icon-bianji"></i>编辑</a>
								<span><i class="iconfont icon-lajixiang"></i>删除</span>
							</label>
						</div>
					</div>-->
					<!--<div class="card">
						<div class="card-content">
							<div class="card-content-inner">
								<p class="user-info"><span class="iconfont icon-iconfontlianxiren01"></span><span>陈锦川</span><span class="iconfont icon-shouji2"></span><span>15602268070</span></p>
								<p class="user-addr">
									<span>广东省</span><span>广州市</span><span>天河区</span><span>银汇大厦26楼蜘点集团技术部</span>
								</p>
							</div>
						</div>
						<div class="card-footer">
							<span class="pull-left">设置默认</span>
							<span class="pull-right">
								<a class="external" href="creat-address.html"><i class="iconfont icon-bianji"></i>编辑</a>
								<span><i class="iconfont icon-lajixiang"></i>删除</span>
							</span>
						</div>
					</div>-->
					<!--<img src="img/nogood2.png" alt="" width="40%" style="margin-top: 80px;" />-->
				</div>
				<!--content end-->

			</div>
		</div>

		<script type="text/javascript" src="js/zepto.js" charset="UTF-8"></script>
		<script type="text/javascript" src="js/sm.min.js" charset="UTF-8"></script>
		<script type="text/javascript" src="js/sm-extend.min.js" charset="UTF-8"></script>
		<script type="text/javascript" src="js/common.js" charset="UTF-8"></script>
		<!--<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>-->
		<script type="text/javascript" src="js/url.js"></script>
		<script src="js/mui.min.js"></script>
		<script type="text/javascript">
//			window.location.reload();
			//页面加载时绑定按钮点击事件
			$('.add-addr').on('tap', function() {
				mui.openWindow({
					url: 'creat-address.html',
					id: 'creat-address.html',
					show: 'pop-in'
				});
			})
			$('.content').on('tap', '.externalxg', function() {
				mui.openWindow({
					url: 'editoraddress.html',
					id: 'editoraddress',
					show: 'pop-in'
				});
				localStorage.externalxg = $(this).parents('.card').find('.det').attr('id');
				localStorage.thaddress = $(this).parents('.card').attr('th');
				//			  alert($(this).parents('.card').find('.det').attr('id'));
				//			  alert($(this).parents('.card').attr('th'));
			});

			showlist();

			function showlist() {

				$.ajax({
					type: 'post',
					url: ur + '/mobilemember/frontmember/listAddress?time='+new Date(),
					headers: {
						"X-Token": localStorage.token
					},
					/*url:'http://wy.tunnel.bbear.me:8000/wyshop/mobilemember/frontmember/saveAddressByAdd',*/
					data: {},
					dataType: 'json',
					success: function(data) {
						// 						plus.nativeUI.closeWaiting();		
						console.log(JSON.stringify(data));
						console.log(data.result.meta.code)
						//						空
						//						var img = $('<img src="img/nogood2.png" alt="" width="40%" style="margin-top: 80px;    margin-left: 30%;" />');
						//						if(data.result.data == null) {
						//							$('.content').append(img);
						//						}
						//						非空
						if(data.result.meta.code == 200) {

							for(var i = 0; i < data.result.data.length; i++) {
								var div = $('<div class="card" th="' + i + '">' +
									'<div class="card-content">' +
									'<div class="card-content-inner">' +
									'<p class="user-info"><span class="iconfont icon-iconfontlianxiren01"></span>' +
									'<span>' + data.result.data[i].reciver + '</span><span class="iconfont icon-shouji2">' +
									'</span><span>' + data.result.data[i].phone + '</span></p>' +
									'<p class="user-addr"><span></span><span>' + data.result.data[i].city + '</span><span>' + data.result.data[i].area + '</span>' +
									'<span>' + data.result.data[i].detail + '</span></p></div></div>' +
									'<div class="card-footer"><span class="pull-left" id=' + 'defau' + data.result.data[i].isDefault + ' ida="' + data.result.data[i].id + '">设置默认</span>' +
									'<span class="pull-right"><a class="externalxg"><i class="iconfont icon-bianji"></i>编辑</a>' +
									'<span class="det" id=' + data.result.data[i].id + '><i class="iconfont icon-lajixiang"></i>删除</span></span></div></div>')
								$('.content').append(div);

								var defau = data.result.data[i].isDefault;
								console.log(defau);
								if(defau == 1) {
									console.log('默认');
									$('.content').find('#defau1').addClass('active');
									$('.content').find('#defau1').text('默认地址');
								}
							}
						} else if(data.result.meta.code == 201) {
							var divimg = ('<div style="margin:5rem auto 0;text-align: center;"><img style="width:35%;" src="img/nogood.png"><p style="color:#6a6a6a;font-size: .8rem;">您暂时还没有收货地址哦</p></div>');
							$('.content').append(divimg);
						}
					},
					error: function(data) {
						//						 plus.nativeUI.closeWaiting();
						console.log(data);
					}
				});
			}

			/*从默认地址删除*/
			$(".content").on('tap', '.det', function() {
				//				alert($(this).parents('.card').find('.pull-left').attr('id'));
				//				if($(this).parents('.card').find('.pull-left').attr('id')=='defau1'){
				//					mui.alert('默认地址不能删除，您可以新增其他默认地址，再删除本条', '魏远商城', function() {
				//					
				//				});
				//				}else if($(this).parents('.card').find('.pull-left').attr('id')=='defau0'){
				var cli = $(this).attr('id');
				var all = $(this).parents('.card');
				var btnArray = ['否', '是'];
				mui.confirm('确认删除，确认？', '魏远商城', btnArray, function(e) {
					if(e.index == 1) {
						$.ajax({
							url: ur + '/mobilemember/frontmember/deleteAddress',
							type: 'post',
							dataType: 'json',
							headers: {
								"X-Token": localStorage.token
							},
							data: {
								"id": cli,
								/*"id": '40288051594f367a015963128b240033',*/
								"size": '0',
							},
							success: function(data) {

								console.log(data);
								all.hide(1000);
								if(data == '操作成功') {
									mui.toast('删除成功');
								} else {
									/*plus.nativeUI.closeWaiting();*/
								}
							}
						})
					}
				})
				//				}
			});

			/*设置为默认地址*/
			$(".content").on('tap', '.pull-left', function() {
				var divdan = $(this);
				var divdano = $(this).parents('.card').siblings().find('.pull-left');
				var addaddrss = $(this).attr('ida');
				console.log(divdano);
				console.log(divdan);
				console.log(addaddrss);
				$.ajax({
					url: ur + '/mobilemember/frontmember/updateAddressById',
					type: 'post',
					dataType: 'json',
					headers: {
						"X-Token": localStorage.token
					},
					data: {
						"addressVO.id": addaddrss,
						/*"id": '40288051594f367a015963128b240033',*/
						"addressVO.isDefault": 1,
					},
					success: function(data) {
						console.log(data);
						console.log(data.result.meta.code);
						if(data.result.meta.code == 200) {

							mui.toast('设置默认地址成功');
							divdan.css({ 'color': '#fff', 'background-color': '#e9af29', 'border': 'none' });
							divdan.text('默认地址');
							divdano.css({ 'color': '#666', 'background-color': 'transparent', 'border': '1px solid #999' });
							divdano.text('设置默认');

							mui.back();
							/*divdano.css({'color': '#666','border': '1px solid #999'});*/
						} else {
							/*plus.nativeUI.closeWaiting();*/
						}
					},
					error: function(data) {
						mui.toast("系统错误");
					}
				})
			});
		</script>
	</body>

</html>